'''
网格管理、事件大小类， 流程管理、执法督察
'''
from Lib.base.page_object.case_flow.case_flow import CaseFlow
from Lib.base.page_object.case_type.case_type import CaseType
from Lib.base.page_object.grid_manager.grid_manager import GridManager
from Lib.base.page_object.enforce_the_law_manager.enforce_the_law_manager import EnforceTheLawManager
from Lib.base.page_object.window_manager.window_manager import WindowManager
from selenium.webdriver.common.by import By
import allure
import os
from Lib.common.assert_class import AssertClass
import pytest
from Lib.common.get_data_by_mysql import MysqlAssert


@pytest.fixture(scope='class', autouse=True)
@pytest.mark.usefixtures('login')
def set_cls(login):
    TestGridFlowCase.caseFlow = CaseFlow(login)
    TestGridFlowCase.caseType = CaseType(login)
    TestGridFlowCase.gridManager = GridManager(login)
    TestGridFlowCase.windowManager = WindowManager(login)
    TestGridFlowCase.case = EnforceTheLawManager(login)
    TestGridFlowCase.assert_class = AssertClass(login)


@allure.feature("网格、事件类型、事件流程、事件处理测试")
class TestGridFlowCase:

    @allure.story("网格管理")
    @allure.title("新建网格")
    def test_1_create_grid(self):
        '''
        新增网格
        :return:
        '''
        self.gridManager.create_grid()
        # 设置断言
        assert_arg = (By.XPATH, '//p[text()="新建成功!"]')
        self.assert_class.make_assert_by_text(args=assert_arg, assert_data='新建成功', context='新建网格')

    @allure.story("事件类型配置")
    @allure.title("新建事件大类")
    def test_2_create_big_case_type(self):
        '''
        新增事件大类
        :return:
        '''
        self.caseType.create_case_big_type()
        # 设置断言
        assert_arg = (By.XPATH, '//p[text()="新建事件大类成功"]')
        self.assert_class.make_assert_by_text(args=assert_arg, assert_data='新建事件大类成功', context='新建事件大类')

    @allure.story("事件类型配置")
    @allure.title("新建事件小类")
    def test_3_create_small_case_type(self):
        '''
        新增事件小类
        :return:
        '''
        self.caseType.create_case_small_type()
        # 设置断言
        assert_arg = (By.XPATH, '//p[text()="新建事件小类成功"]')
        self.assert_class.make_assert_by_text(args=assert_arg, assert_data='新建事件小类成功', context='新建事件小类')

    @allure.story("事件流程管理")
    @allure.title("新建事件流程")
    def test_4_create_case_flow(self):
        '''
        新增事件流程
        :return:
        '''
        self.caseFlow.create_flow()
        # 设置断言
        assert_arg = (By.XPATH, '//p[text()="成功新建流程"]')
        self.assert_class.make_assert_by_text(args=assert_arg, assert_data='成功新建流程', context='新建事件流程')

    @allure.story("督察管理")
    @allure.title("新建事件")
    def test_5_create_case(self):
        '''
        新增事件
        :return:
        '''
        self.case.create_case()
        # 设置断言
        assert_args = (By.XPATH, '//tbody/tr[1]/td[1]//span')
        self.assert_class.make_assert_by_text(args=assert_args, assert_data="UI新建事件编号", context="事件上报")

    @allure.story("督察管理")
    @allure.title("查看事件列表")
    def test_6_case_list(self):
        '''
        事件列表
        :return:
        '''
        self.case.case_list()
        # 设置断言
        assert_arg1 = (By.XPATH, '//table[@class="rz-table__body"]/tbody//tr[1]/td[4]/div')
        assert_arg2 = (By.XPATH, '//table[@class="rz-table__body"]/tbody//tr[2]/td[4]/div')
        self.assert_class.make_assert_by_compare_time(args1=assert_arg1, args2=assert_arg2, context="事件列表")

    @allure.story("督察管理")
    @allure.title("事件受理")
    def test_7_case_accept(self):
        '''
        事件受理
        :return:
        '''
        self.case.case_accept()
        # 设置断言
        assert_arg = (By.XPATH, '//p[text()="操作成功"]')
        self.assert_class.make_assert_by_text(args=assert_arg, assert_data='操作成功', context='事件受理')

    @allure.story("督察管理")
    @allure.title("事件接单")
    def test_8_case_dispatch(self):
        '''
        事件接单
        :return:
        '''
        self.case.case_dispatch()
        # 设置断言
        assert_arg = (By.XPATH, '//p[text()="操作成功"]')
        self.assert_class.make_assert_by_text(args=assert_arg, assert_data='操作成功', context='事件接单')

    @allure.story("督察管理")
    @allure.title("事件处置")
    def test_9_0_case_deal(self):
        '''
        事件处置
        :return:
        '''
        self.case.case_deal()
        # 设置断言
        assert_arg = (By.XPATH, '//p[text()="操作成功"]')
        self.assert_class.make_assert_by_text(args=assert_arg, assert_data='操作成功', context='事件处置')

    @allure.story("督察管理")
    @allure.title("事件核查和归档")
    def test_9_1_case_check_and_archive(self):
        '''
        事件核查和归档
        :return:
        '''
        self.case.case_check_and_archive()
        # 设置断言
        assert_arg = (By.XPATH, '//p[text()="操作成功"]')
        self.assert_class.make_assert_by_text(args=assert_arg, assert_data='操作成功', context='事件核查和归档')

    @allure.story("窗口管理")
    @allure.title("新增窗口事件")
    def test_9_2_window_case_create(self):
        '''
        窗口管理页面页面新建事件
        :return:
        '''
        self.windowManager.create_window_case()
        # 设置断言
        assert_args = (By.XPATH, '//tbody/tr[1]/td[1]//span')
        self.assert_class.make_assert_by_text(args=assert_args, assert_data='UI新建的窗口事件编号', context='窗口管理事件上报')

    @allure.story("窗口管理")
    @allure.title("窗口事件列表")
    def test_9_3_window_case_list(self):
        '''
        窗口管理页面查询列表页面
        :return:
        '''
        self.windowManager.window_case_list()
        # 设置断言
        assert_arg1 = (By.XPATH, '//table[@class="rz-table__body"]/tbody//tr[1]/td[4]/div')
        assert_arg2 = (By.XPATH, '//table[@class="rz-table__body"]/tbody//tr[2]/td[4]/div')
        self.assert_class.make_assert_by_compare_time(args1=assert_arg1, args2=assert_arg2, context="事件列表")

    @allure.story("事件流程管理")
    @allure.title("事件流程列表")
    def test_9_4_case_flow_list(self):
        '''
        流程列表
        :return:
        '''
        self.caseFlow.flow_list()
        # 设置断言
        assert_arg = (By.XPATH, '//span[text()="查看流程"]')
        self.assert_class.make_assert_by_text(args=assert_arg, assert_data='查看流程', context='查看流程')

    @allure.story("事件流程管理")
    @allure.title("修改事件流程")
    def test_9_5_case_flow_update(self):
        '''
        修改流程
        :return:
        '''
        self.caseFlow.flow_search_update()
        # 设置断言
        assert_arg = (By.XPATH, '//p[contains(text(),"成功编辑流程")]')
        self.assert_class.make_assert_by_text(args=assert_arg, assert_data='成功编辑流程', context='编辑流程')

    @allure.story("事件流程管理")
    @allure.title("删除事件流程")
    def test_9_6_case_flow_delete(self):
        '''
        删除流程
        :return:
        '''
        self.caseFlow.flow_search_delete()
        # 设置断言
        assert_arg = (By.XPATH, '//p[contains(text(),"删除成功")]')
        self.assert_class.make_assert_by_text(args=assert_arg, assert_data='删除成功', context='删除流程')

    @allure.story("事件配置")
    @allure.title("修改事件小类")
    def test_9_7_case_small_type_update(self):
        '''
        修改事件小类
        :return:
        '''
        self.caseType.update_case_small_type()
        # 设置断言
        assert_arg = (By.XPATH, '//p[contains(text(),"更新事件小类成功")]')
        self.assert_class.make_assert_by_text(args=assert_arg, assert_data='更新事件小类成功', context='更新事件小类')

    @allure.story("事件配置")
    @allure.title("删除事件小类")
    def test_9_8_case_small_type_delete(self):
        '''
        删除事件小类
        :return:
        '''
        self.caseType.delete_case_small_type()
        # 设置断言
        assert_arg = (By.XPATH, '//p[contains(text(),"删除事件小类成功")]')
        self.assert_class.make_assert_by_text(args=assert_arg, assert_data='删除事件小类成功', context='删除事件小类')

    @allure.story("事件配置")
    @allure.title("修改事件大类")
    def test_9_9_0_case_big_type_update(self):
        '''
        修改事件大类
        :return:
        '''
        self.caseType.update_case_big_type()
        # 设置断言
        assert_arg = (By.XPATH, '//p[contains(text(),"更新事件大类成功")]')
        self.assert_class.make_assert_by_text(args=assert_arg, assert_data='更新事件大类成功', context='更新事件大类')

    @allure.story("事件配置")
    @allure.title("删除事件大类")
    def test_9_9_1_case_big_type_delete(self):
        '''
        删除事件大类
        :return:
        '''
        self.caseType.delete_case_big_type()
        # 设置断言
        assert_arg = (By.XPATH, '//p[contains(text(),"删除事件大类成功")]')
        self.assert_class.make_assert_by_text(args=assert_arg, assert_data='删除事件大类成功', context='删除事件大类')

    @allure.story("网格管理")
    @allure.title("修改网格")
    def test_9_9_2_grid_update(self):
        '''
        编辑网格
        :return:
        '''
        self.gridManager.update_grid()
        assert_arg = (By.XPATH, '//label[text()="更新时间"]/../div/span')
        self.assert_class.make_assert_by_text_is_null(args=assert_arg, context="编辑网格")

    @allure.story("网格管理")
    @allure.title("删除网格")
    def test_9_9_3_case_grid_delete(self):
        '''
        删除网格
        :return:
        '''
        self.gridManager.delete_grid()
        # 设置断言
        assert_arg = (By.XPATH, '//p[contains(text(),"删除成功")]')
        self.assert_class.make_assert_by_text(args=assert_arg, assert_data='删除成功', context='删除网格')

    @allure.story("督察管理")
    @allure.title("新增自发自处事件")
    def test_create_case_deal_with_self(self):
        '''
        删除网格
        :return:
        '''
        self.case.case_deal_with_self()
        # 设置断言
        assert_arg = (By.XPATH, '//p[contains(text(),"事件上报成功")]')
        self.assert_class.make_assert_by_text(args=assert_arg, assert_data='事件上报成功')


if __name__ == '__main__':
    pytest.main(['-s', '-v', './grid_flow_case_test.py::TestGridFlowCase'])
    # pytest.main(['-v', './grid_flow_case_test.py', '--alluredir', '../result', '--clean-alluredir'])
    # os.system(r'copy ..\config\environment.properties ..\result\environment.properties')
    # os.system('allure generate  ../result -o ../report/ --clean')
